// eslint-disable-next-line no-restricted-globals
self.onmessage = (e) => {
  const { baseUrl, allLines, segmentLength } = e.data;
  // eslint-disable-next-line no-undef
  importScripts(`${baseUrl}/lib/turf.js`);

  if (!allLines) {
    throw new Error('allLines线路不存在');
  }

  if (allLines && allLines.length) {
    for (let i = 0; i < allLines.length; i++) {
      const currnet = allLines[i];
      const uniqLines = [...new Set(currnet.lineList.map(JSON.stringify))].map(JSON.parse);
      const line = turf.lineString(uniqLines);
      const lineChunk = turf.lineChunk(line, segmentLength);
      const result = turf.coordAll(lineChunk);
      currnet.lineChunk = [...new Set(result.map(JSON.stringify))].map(JSON.parse);
    }
  }

  // eslint-disable-next-line no-restricted-globals
  self.postMessage(allLines);
};
